/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			mainwindow.h
  \date			Jul 2011
  \author		TNick

  \brief		Contains the definition for MainWindow class


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n
 Please read ReadMe.txt and Licence.txt in root folder @n
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
#ifndef __MAINWINDOW_INC__
#define __MAINWINDOW_INC__
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

class       MainWindow;

#include    <QMainWindow>
#include    <QMdiSubWindow>
#include    <QPrinter>
#include    <QFileInfo>

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/// form representing a document inside MDI area
/**
*   @todo rise selStateChange() when the selection changes
*/
class MdiChild : public QMdiSubWindow		{
    Q_OBJECT

    //
    //
    //
    //
    /*  DEFINITIONS    ----------------------------------------------------- */

    /*  DEFINITIONS    ===================================================== */
    //
    //
    //
    //
    /*  DATA    ------------------------------------------------------------ */

private:

    /// full path and name for the file hosted by this document
    QString     cur_file;

    /// tell if this is a new file or one loaded form disk
    bool        is_untitled;

    /// tell if the document was modified (and, this, needs saving)
    bool        is_modified;

    /// (bug?) workarround
    /**
*   it looks like the class instance does not gets deleted or removed from
*   MDI area (but, visually, it is closed). This is set when a close
*   signal is accepted. Next time MainWindo finds an instance that was closed
*   it will delete it, removing this way from MDI chain, too.
*/
    bool        is_closed;


    /* some systems (like symbian) have no support for printing */
#ifndef QT_NO_PRINTER
    /// printer configuration structure
    QPrinter    printer;
#endif

    /*  DATA    ============================================================ */
    //
    //
    //
    //
    /*  FUNCTIONS    ------------------------------------------------------- */

public:

    /// constructor
    MdiChild    (void);

    /// initialises the instance to represent a new document
    void        newFile                     (void);

    /// allows for browsing and loads the file in the instance
    bool        loadFile                    (const QString &fileName);

    /// high level; saves the document (may call saveAs())
    bool        save                        (void);

    /// shows a dialog allowing to select where to save the document
    bool        saveAs                      (void);

    /// effectively saving the file
    bool        saveFile                    (const QString &fileName);

    /// strips the path from file name
    QString     userFriendlyCurrentFile     (void)
    { return strippedName(cur_file); }

    /// tells the full path and name of the file hosted by this document
    QString     currentFile                 (void)
    {
        if (is_untitled)
            return QString();
        else
            return cur_file;
    }

    /// tell if this document has anything selected
    bool        hasSelection                (void)
    { return false; }



    /// undo last action in document
    void        undo            	(void) {}

    /// redo last action in document
    void        redo            	(void) {}

    /// cut the selection
    void        cut                 (void) {}

    /// copy the selection
    void        copy            	(void) {}

    /// paste content of the clipboard
    void        paste            	(void) {}

    /// delete the selection
    void        delSel           	(void) {}

    /// select entire content of the document
    void        selAll            	(void) {}

    /// tell if this document was closed
    /**
*   it looks like the class instance does not gets deleted or removed from
*   MDI area (but, visually, it is closed). This is set when a close
*   signal is accepted. Next time MainWindo finds an instance that was closed
*   it will delete it, removing this way from MDI chain, too.
*/
    bool        closed              (void)
    { return is_closed; }

    /// presents the print dialog; user is allowed to print
    void        printDialog         (void);

    /// presents the page setup dialog
    void        pageSetupDialog     (void);

    /// preview printed document
    void        printPreviewDialog  (void);

    /// actually do the printing
    void        print               (void);




protected:
    /// reimplements the event to allow saving the changes
    void        closeEvent          (QCloseEvent *event);



private slots:
    /// central point that is informed about changes
    void        documentWasModified (void)
    { is_modified = true; }



signals:
    /// raised when saving under a different name
    void        nameChanged         (void);
    /// raised when something is selected / the selection is cleared
    void        selStateChange      (void);
    /// raised when a document is closed
    void        docClosed           (MdiChild* p_closed);



private:

    /// dialog based routine to decide if closing may proceed
    bool        maybeSave           (void);

    /// save the path and name of the file; set internal state
    void        setCurrentFile      (const QString &fileName);

    /// strips the path from file name
    QString     strippedName        (const QString &fullFileName)
    { return QFileInfo(fullFileName).fileName(); }



    /*  FUNCTIONS    ======================================================= */
    //
    //
    //
    //

};	/*	class MdiChild	*/
/*  CLASS    =============================================================== */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

namespace Ui {
class MainWindow;
}

/// form representing main panel for application
class MainWindow : public QMainWindow		{
    Q_OBJECT
    //
    //
    //
    //
    /*  DEFINITIONS    ----------------------------------------------------- */

    /*  DEFINITIONS    ===================================================== */
    //
    //
    //
    //
    /*  DATA    ------------------------------------------------------------ */

private:

    /// GUI components
    Ui::MainWindow *        ui;

    /// recent files list
    QList<QString>          rcnt_f;

    /// number of recent files to store
    int                     rcnt_f_count;


    /*  DATA    ============================================================ */
    //
    //
    //
    //
    /*  FUNCTIONS    ------------------------------------------------------- */

public:

    /// constructor;
    explicit			MainWindow		(QWidget *parent = 0);

    /// destructor;
    ~MainWindow		(void);



protected:

    /// reimplement the event handler to allow saving changes before
    void    closeEvent              (QCloseEvent *event);

    /// places provided file to the top of recent files list
    void    addRecentFile           (QString s_file);



private slots:
    /// initialises the instance to represent a new document
    void    slot_New            	(void);

    /// allows for browsing and loads the file in the instance
    void    slot_Open            	(void);

    /// high level; saves the document (may call saveAs())
    void    slot_Save            	(void);

    /// iterates in all opened documents and save them as necessary
    void    slot_SaveAll            (void);

    /// shows a dialog allowing to select where to save the document
    void    slot_SaveAs            	(void);

    /// presents the print dialog; user is allowed to print
    void    slot_Print            	(void);

    /// presents the page setup dialog for active document
    void    slot_PrintStUp          (void);

    /// preview printed active document
    void    slot_PrintPreview       (void);

    /// undo last action in active document
    void    slot_Undo            	(void);

    /// redo last action in active document
    void    slot_Redo            	(void);

    /// cut the selection in active document
    void    slot_Cut            	(void);

    /// copy the selection in active document
    void    slot_Copy            	(void);

    /// paste content of the clipboard in active document
    void    slot_Paste            	(void);

    /// delete the selection in active document
    void    slot_Delete            	(void);

    /// select entire content of the active document
    void    slot_SelAll            	(void);

    /// removes the color decoration that may have been previously loaded
    void    slot_No_skin            (void);

    /// loads color decoration from a file (the user needs to browse)
    void    slot_LoadStySh          (void);

    /// toogle layout direction (left to right or reverse)
    void    slot_LayDir             (void);

    /// presents a short message describing the application
    void    slot_About            	(void);

    /// provides access to the help system
    void    slot_Help            	(void);

    /// contextual help for GUI
    void    slot_HelpCtx            (void);

    /// change the template for the GUI
    void    slot_UItplChange        (QAction* p_active);

    /// change the color scheme of the GUI
    void    slot_SkinChange         (QAction* p_active);

    /// informed when active document has changed
    void    slot_ActiveMdiChange    (QMdiSubWindow* p_active);

    /// informed when active document has changed
    void    slot_ActiveMdiChange    (MdiChild* p_active);

    /// used by the windows menu to change active document
    void    slot_ActiveMdiChange    (QAction* p_active);

    /// used to open a recent file from the menu
    void    slot_RecentFile         (QAction* p_active);

    /// recreates the list of windows currently opened in MDI
    void    slot_UpdateWinMenu      (void);

    /// recreates the list of recent files in the menu
    void    slot_UpdateRcFilesMenu  (void);

    /// is informed when the selection in one of the documents changes
    void    slot_SelStateChange     (void);




    /// create a new document in MDI
    MdiChild *          createMdiChild      (void);

    /// tell which is the active document in MDI
    MdiChild *          activeMdiChild      (void);

    /// finds associated document for a given file
    QMdiSubWindow *     findMdiChild        (const QString &fileName);

    /// checks for closed panel and deletes them
    void    cleanMDILeftovers       (void);

    /// actual open function used by recent files and regular open
    bool    _open                   (QString s_file);
    /// actual loading for style sheets
    void    _loadStySh              (QString fileName);



    /*  FUNCTIONS    ======================================================= */
    //
    //
    //
    //

};	/*	class MainWindow	*/

/*  CLASS    =============================================================== */
//
//
//
//
#endif // __MAINWINDOW_INC__
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
